package service;

import mapper.RecordMapper;
import org.apache.ibatis.session.SqlSession;
import util.DBUtil;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * 核心业务
 */
public class CoreService {
    /**
     * 预约
     * @param id 读者id
     * @param floor_name 楼层名
     * @param number 座位号
     * @return Timestamp
     */
    public Timestamp reserve(String id, String floor_name, int number) {
        SqlSession sqlSession = null;
        Timestamp reserve_time = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RecordMapper mapper = sqlSession.getMapper(RecordMapper.class);
            reserve_time = mapper.reserve(id, floor_name, number);
            sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return reserve_time;
    }

    /**
     * 取消
     * @param floor_name 楼层名
     * @param number 座位号
     * @param reserve_time 预约时间
     */
    public void cancelReservation(String floor_name, int number, String reserve_time) {
        SqlSession sqlSession = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RecordMapper mapper = sqlSession.getMapper(RecordMapper.class);
            mapper.cancelReservation(floor_name, number, reserve_time);
            sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
    }

    /**
     * 签到
     * @param floor_name 楼层名
     * @param number 座位号
     * @param reserve_time 预约时间
     * @return Timestamp
     */
    public Timestamp startUsing(String floor_name, int number, String reserve_time) {
        SqlSession sqlSession = null;
        Timestamp start_time = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RecordMapper mapper = sqlSession.getMapper(RecordMapper.class);
            start_time = mapper.startUsing(floor_name, number, reserve_time);
            sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return start_time;
    }

    /**
     * 结束
     * @param floor_name 楼层名
     * @param number 楼层号
     * @param reserve_time 预约时间
     * @return Timestamp
     */
    public Timestamp endUsing(String floor_name, int number, String reserve_time) {
        SqlSession sqlSession = null;
        Timestamp endTime = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RecordMapper mapper = sqlSession.getMapper(RecordMapper.class);
            endTime = mapper.endUsing(floor_name, number, reserve_time);
            sqlSession.commit();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return endTime;
    }
}
